/*
 * Copyright (C) 2018 yixun All Rights Reserved.
 * 
 * SmsSettingServiceImpl.java
 */
package com.yixun.qfbao.service.impl;

import com.yixun.common.page.Page;
import com.yixun.qfbao.mapper.SmsSettingMapper;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.SmsSetting;
import com.yixun.qfbao.model.SmsTemplate;
import com.yixun.qfbao.model.dto.CompanyDto;
import com.yixun.qfbao.model.dto.SmsSettingDto;
import com.yixun.qfbao.model.dto.SmsTemplateDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.service.CompanyService;
import com.yixun.qfbao.service.SmsSettingService;
import com.yixun.qfbao.service.SmsTemplateService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 * [文件名称]<br>
 * SmsSettingServiceImpl.java<br>
 * <br>
 * [文件描述]<br>
 * 主键Id service interface impl<br>
 * <br>
 * [修改记录]<br>
 * 2018-06-13 14:11:20 创建 陶俊代码生成器<br>
 * 
 * @author 陶俊代码生成器
 * @version 1.00
 */
@Service("smsSettingService")
public class SmsSettingServiceImpl implements SmsSettingService {
	
	@Autowired
	private SmsSettingMapper mapper;
	@Autowired
	private CompanyService companyService;
	@Autowired
	private SmsTemplateService smsTemplateService;
	/**
	 * @param entity 搜索条件
	 * @param begin 分页开始行数[不包含]
	 * @param end 分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<SmsSetting> getPageModel(SmsSettingDto entity,int begin,int end){
		Integer total = mapper.count(entity);
		List<SmsSetting> rows = mapper.list(entity,begin,end);
	    Page<SmsSetting> msr = new Page<SmsSetting>(total, rows);
		return msr;
	}

	/**
	 * 查询列表
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public List<SmsSetting> selectListByDto(SmsSettingDto entity){
		return mapper.selectListByDto(entity);
	}
	
	/**
	 * 查询单个
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public SmsSetting selectByDto(SmsSettingDto entity){
		return mapper.selectByDto(entity);
	}
	
	/**
	 * @param id 搜索条件
	 * @return 分页记录
	 */
	public SmsSetting selectById(Long id){
		if(id!=null){
			return mapper.selectById(id);
		}
		return null;
	}
	/**
	 * @param entity 新增记录的详细信息
	 * @return 新增行数
	 */
	public SmsSettingDto create(SmsSettingDto entity) {
		entity.setCreateTime(Calendar.getInstance().getTime());
		mapper.add(entity);
		return entity;
	}

	/**
	 * @param entity 更新记录的详细信息
	 * @return 更新行数
	 */
	public int update(SmsSettingDto entity) {
		entity.setUpdateTime(Calendar.getInstance().getTime());
		return mapper.edit(entity);
	}
    
	/**
	 * @param entity 待删除的记录信息
	 * @return 删除行数
	 */
	public int destroy(SmsSettingDto entity) {
		return mapper.del(entity);
	}
	
	/**
	 * @param entity 新增或更新
	 * @return 新增行数
	 */
	public int saveOrUpdate(SmsSettingDto entity) {
		if(entity!=null&&entity.getId()!=null){
			return update(entity);
		}else{
			create(entity);
			return 1;
		}
	}
	
	/**
	 * @param entity 根据pojo搜索如果主键为空则返回它本身
	 * @return 一行记录或本身
	 */
	@Override
	public SmsSetting selectBy(SmsSetting entity) {
		if(entity!=null&&entity.getId()!=null){
			return mapper.selectById(entity.getId());
		}
		return entity;
	}

	@Override
	public Map<String, String> getSmsSettingMap(String domain, Long higherId,String companyUuid) {
		Map<String, String> smsSettingMap = null;
		CompanyDto companyDto = new CompanyDto();
		if(higherId != null ){
			companyDto.setId(higherId);
		}else if(StringUtils.isNotBlank(companyUuid)) {
        	companyDto.setCompanyUuid(companyUuid);
		}else if(StringUtils.isNotBlank(domain)){
			companyDto.setDomain(domain);
		}else{
			throw new RuntimeException("公司信息获取失败");
		}
		Company company = companyService.selectByDto(companyDto);
		if(null != company) {
			higherId = company.getId();
		}else{
			throw new RuntimeException("公司信息获取失败");
		}

		SmsSettingDto ssDto = new SmsSettingDto();
		ssDto.setHigherid(higherId);
		ssDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		SmsSetting ss = mapper.selectByDto(ssDto);
		if(null != ss) {
			smsSettingMap = new HashMap<String, String>();
			smsSettingMap.put("platform", ss.getPlatform());
			smsSettingMap.put("userName", ss.getSmsAccount());
			smsSettingMap.put("pwd", ss.getSmsApiPwd());
			smsSettingMap.put("sign", ss.getSmsSign());
			smsSettingMap.put("smsurl", ss.getSmsUrl());
		}
		
		return smsSettingMap;
	}

	@Override
	public Map<String, String> getSmsTempSetting(Long higherId, String tempType) {
		Map<String, String> smsSettingMap = null;
		SmsSettingDto ssDto = new SmsSettingDto();
		ssDto.setHigherid(higherId);
		ssDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		SmsSetting ss = mapper.selectByDto(ssDto);
		if(null != ss) {
			smsSettingMap = new HashMap<String, String>();
			smsSettingMap.put("platform", ss.getPlatform());
			smsSettingMap.put("userName", ss.getSmsAccount());
			smsSettingMap.put("subUserName", ss.getSmsSubAccount());
			smsSettingMap.put("pwd", ss.getSmsApiPwd());
			smsSettingMap.put("sign", ss.getSmsSign());
			smsSettingMap.put("smsurl", ss.getSmsUrl());
		}

		SmsTemplateDto dto = new SmsTemplateDto();
		dto.setHigherId(higherId);
		dto.setTempType(tempType);
		dto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		SmsTemplate smsTemplate = smsTemplateService.selectByDto(dto);
		if(Objects.nonNull(smsTemplate)){
			smsSettingMap.put("tempUuid", smsTemplate.getTempUuid());
			smsSettingMap.put("tempNo", smsTemplate.getTempNo());
			smsSettingMap.put("tempName", smsTemplate.getTempName());
			smsSettingMap.put("tempContent", smsTemplate.getTempContent());
		}

		return smsSettingMap;
	}
}
